package jn.chen.utils.fileDownLoad;

import jn.chen.utils.fileDownLoad.utils.DownloadService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @className: GifController
 * @author: chenLin
 * @version: 1.0.0
 * @description:
 * @date: 2022/11/2 0002 10:46
 */
@RestController
@Validated
public class GifController {

    @Resource
    private DownloadService downloadService;
    /**
     * 下载网上文件 {传统io stream}
     * @param url 网络图片url
     * @param saveDir 本地存放路径{C:/img}格式
     * @param fileName 本地存放文件名称
     */
    @PostMapping("/download")
    @ResponseBody
    public void download(@RequestParam("url") String url,@RequestParam("saveDir") String saveDir,@RequestParam("fileName") String fileName) {
         downloadService.download(url, saveDir, fileName);
    }

    /**
     * 利用 commonio 库下载文件，依赖Apache Common IO ，官网 https://commons.apache.org/proper/commons-io/
     * @param url 网络图片url
     * @param saveDir 本地存放路径{C:/img}格式
     * @param fileName 本地存放文件名称
     */
    @PostMapping("/downloadByApacheCommonIO")
    @ResponseBody
    public  void downloadByApacheCommonIO(@RequestParam("url") String url,@RequestParam("saveDir") String saveDir,@RequestParam("fileName") String fileName) {
        downloadService.downloadByApacheCommonIO(url, saveDir, fileName);
    }


    /**
     * 使用NIO下载文件， 需要 jdk 1.7+
     * @param url 网络图片url
     * @param saveDir 本地存放路径{C:/img}格式
     * @param fileName 本地存放文件名称
     */
    @PostMapping("/downloadByNIO")
    @ResponseBody
    public void downloadByNIO(@RequestParam("url") String url,@RequestParam("saveDir") String saveDir,@RequestParam("fileName") String fileName) {
        downloadService.downloadByNIO(url, saveDir, fileName);
    }
}
